package ru.ifmo.network;

import javax.swing.*;
import java.awt.*;

/**
 * @author avhaliullin
 */
public class SwingUtils {

    public static JComponent getBox(boolean vertical, Component... components) {
        Box box = vertical ? Box.createVerticalBox() : Box.createHorizontalBox();
        for (Component component : components) {
            box.add(component);
        }
        return box;
    }

    public static JComponent getBox(boolean vertical, String title, Component... components) {
        Box box = vertical ? Box.createVerticalBox() : Box.createHorizontalBox();
        for (Component component : components) {
            box.add(component);
        }
        box.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder(title),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        return box;
    }

    public static JComponent getLabeledComponent(JComponent component, String label) {
        Box box = Box.createHorizontalBox();
        box.add(new JLabel(label));
        box.add(component);
        return box;
    }

    public static void invokeLaterIfNotEDT(Runnable task) {
        if (SwingUtilities.isEventDispatchThread()) {
            task.run();
        } else {
            SwingUtilities.invokeLater(task);
        }
    }

}
